﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel, Folder;

    Folder = function (data) {
        var self = this;
        self.id = ko.observable(data.id);
        self.folderId = ko.observable(data.folderId);
        self.name = ko.observable(data.name);
        self.tags = ko.observable(data.tags);
        self.description = ko.observable(data.description);
    };

    ViewModel = function (data) {
        var self = this;
        var accountLine = data.detail;

        var canvas = document.getElementById("imgCanvas");
        util.image.drawImage(canvas, util.image.checkOrDefault(accountLine.imageData));

        self.id = ko.observable(accountLine.id);
        self.name = ko.observable(accountLine.name);
        self.accountNumber = ko.observable(accountLine.accountNumber);
        self.type = ko.observable(accountLine.accountTypeName);
        self.industry = ko.observable(accountLine.industryName);
        self.billingStreet = ko.observable(accountLine.billingStreet);
        self.billingCity = ko.observable(accountLine.billingCity);
        self.billingState = ko.observable(accountLine.billingState);
        self.billingCountry = ko.observable(accountLine.billingCountry);
        self.billingPostalCode = ko.observable(accountLine.billingPostalCode);
        self.shippingStreet = ko.observable(accountLine.shippingStreet);
        self.shippingCity = ko.observable(accountLine.shippingCity);
        self.shippingState = ko.observable(accountLine.shippingState);
        self.shippingCountry = ko.observable(accountLine.shippingCountry);
        self.shippingPostalCode = ko.observable(accountLine.shippingPostalCode);
        self.phone = ko.observable(accountLine.phone);
        self.mobile = ko.observable(accountLine.mobile);
        self.email = ko.observable(accountLine.email);
        self.website = ko.observable(accountLine.website);
        self.fax = ko.observable(accountLine.fax);
        self.description = ko.observable(accountLine.description);
        self.imageData = ko.observable(accountLine.imageData);
        
        //for folder assigned
        self.folders = ko.observableArray([]);
        $.each(data.folders, function (row, val) {
            self.folders.push(new Folder(val));
        });    

        self.deleteFolderRecord = function (record) {
            if (!confirm("Are you sure you want to delete this record?")) return;
            ajax.del('folder', record.id(), { async: false }).done(function () {
                self.folders.remove(record);
                return;
            });
        };

        self.addFolder = function () {
            window.location = "/folder/add";
        };
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});